package com.wfcm.wxUitls;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wfcm.utils.WxUtil;

/** 
 * 定时获取微信jsapiTicket的线程 
 *  
 * @author liuyq 
 * @date 2013-05-02 
 */  
public class JsapiTicket implements Runnable {  
    private static Logger log = LoggerFactory.getLogger(JsapiTicket.class);  
    // 第三方用户唯一凭证  
    public static String appid = "";  
    
    public static AccessToken jsapiTicket = null;  
    
    public void run() {  
        while (true) {  
            try {              	
            	String accessToken = null;
            	while(StringUtils.isBlank(accessToken)) {
            		Thread.sleep(1 * 1000);
            		//accessToken = TokenThread.accessToken.getAccessToken();
            	}
            	//	jsapiTicket = WxUtil.getTicket(accessToken);
                if (null != jsapiTicket) {  
                    log.info("获取jsapiTicket成功，有效时长{}秒 token:{}", jsapiTicket.getExpirationTime(), jsapiTicket.getAccessToken()); 
                    // 休眠7000秒  
                    Thread.sleep((jsapiTicket.getExpirationTime() - 200) * 1000);  
                } else {  
                    // 如果jsapiTicket为null，60秒后再获取  
                    Thread.sleep(60 * 1000);  
                }  
            } catch (InterruptedException e) {  
                try {  
                    Thread.sleep(60 * 1000);  
                } catch (InterruptedException e1) {  
                    log.error("{}", e1);  
                }  
                log.error("{}", e);  
            }  
        }  
    }  
}  
